package com.ptt.sqm.component;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

import com.ptt.sqm.model.Station;
import com.ptt.sqm.util.AppUtil;
import com.ptt.sqmonmobile.R;

public class StationAdapter extends ArrayAdapter<Station> {
	private Activity context;
	private ArrayList<Station> data = new ArrayList();
	private ArrayList<Station> dataAll = new ArrayList();

	public StationAdapter(Activity context, int resource, ArrayList<Station> data) {
		super(context, resource, data);
		this.context = context;
		this.data = (ArrayList<Station>) data.clone();
		this.dataAll = (ArrayList<Station>) data.clone();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Station getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View row = convertView;
		if (row == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			row = inflater.inflate(R.layout.station_spinner, parent, false);
		}

		Station item = data.get(position);

		if (item != null) { // Parse the data from each object and set it.
			TextView mStationInfo = (TextView) row.findViewById(R.id.station_info);
			TextView mLocationInfo = (TextView) row.findViewById(R.id.location_info);
			TextView mAddressInfo = (TextView) row.findViewById(R.id.address_info);
			if (mStationInfo != null)
				mStationInfo.setText(item.Ship_To + AppUtil.SPACE + AppUtil.PIPE + AppUtil.SPACE + item.Ship_To_Name);
			if (mLocationInfo != null)
				mLocationInfo.setText(item.LOCATION_ID + AppUtil.SPACE + AppUtil.PIPE + AppUtil.SPACE + item.LOCATION_NAME);
			if (mAddressInfo != null)
				mAddressInfo.setText(item.LOCATIONADDRESS);
		}

		return row;
	}

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		Filter myFilter = new Filter() {

			@Override
			protected FilterResults performFiltering(CharSequence constraint) {

				FilterResults filterResults = new FilterResults();

				if (constraint != null) {

					List<Station> filteredProducts = new ArrayList<Station>();

					for (Station item : dataAll) {

						String stationInfo = item.Ship_To + AppUtil.SPACE + AppUtil.PIPE + AppUtil.SPACE + item.Ship_To_Name;

						if (stationInfo.contains(constraint)) {

							filteredProducts.add(item);

						} else {

							String locationInfo = item.LOCATION_ID + AppUtil.SPACE + AppUtil.PIPE + AppUtil.SPACE + item.LOCATION_NAME;

							if (locationInfo.contains(constraint)) {

								filteredProducts.add(item);

							}
						}

					}

					// Now assign the values and count to the FilterResults
					// object

					filterResults.values = filteredProducts;

					filterResults.count = filteredProducts.size();
				}

				return filterResults;

			}

			@Override
			protected void publishResults(CharSequence contraint, FilterResults results) {
				ArrayList<Station> filteredList = (ArrayList<Station>) results.values;
				if (results != null && results.count > 0) {
					data.clear();
					for (Station filter : filteredList) {
						data.add(filter);
					}
					notifyDataSetChanged();

				}

				else {
					notifyDataSetInvalidated();

				}

			}

		};

		return myFilter;

	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
		if (observer != null) {
			super.unregisterDataSetObserver(observer);
		}
	}

}
